package com.news.servlet;


import com.news.pojo.Topic;
import com.news.service.TopicService;
import com.news.service.impl.TopicServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet("/topic.do")
public class TopicServlet extends HttpServlet {
    private TopicService topicService = new TopicServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try{
            //2.设置响应内容格式
            response.setContentType("text/html;charset=UTF-8");
            //3.控制请求的路由
            String opr = request.getParameter("opr");
            switch(opr){
                case "add":{
                    add(request,response);
                    break;
                }
                case "del":{
                    del(request,response);
                    break;
                }
                case "update":{
                    update(request,response);
                    break;
                }
                case "getAll":{
                    getAll(request,response);
                    break;
                }
                case "checkName":{
                    checkName(request,response);//ajax
                    break;
                }
                default:{
                    response.sendRedirect("/404.jsp");
                }
            }
        }catch(Exception e){
            e.printStackTrace();
            response.sendRedirect("/500.jsp");
        }

    }
    /**
     * 检查主题是否存在 ajax
     * @param request
     * @param response
     */
    public void checkName(HttpServletRequest request,HttpServletResponse response) throws SQLException,IOException {
        //service处理页面
        String tname = request.getParameter("tname");
        //响应
        PrintWriter out = response.getWriter();
        out.print((topicService.getTopicByName(tname) == null) ? "true" : "false");//true:可用  false:不可用
        out.flush();
        out.close();
    }
    /**
     * 新增主题
     * @param request
     * @param response
     */
    public void add(HttpServletRequest request,HttpServletResponse response) throws SQLException,IOException {
        //service处理页面
        String tname = request.getParameter("tname");
        Topic topic = new Topic();
        topic.setTname(tname);
        //响应
        HttpSession session = request.getSession();
        session.setAttribute("topic_info",topicService.addTopic(topic));
        response.sendRedirect("/admin/topic_add.jsp");
    }

    /**
     * 删除主题
     * @param request
     * @param response
     */
    public void del(HttpServletRequest request,HttpServletResponse response) throws SQLException,IOException {
        String param = request.getParameter("tid");
        int tid = Integer.valueOf(param);
        //删除结果
        request.getSession().setAttribute("topicInfo",topicService.delTopic(tid));
        response.sendRedirect("/topic.do?opr=getAll");
    }

    /**
     * 修改主题
     * @param request
     * @param response
     */
    public void update(HttpServletRequest request,HttpServletResponse response) throws SQLException,IOException {
        //接受参数
        Topic topic = new Topic();
        topic.setTid(Integer.valueOf(request.getParameter("tid")));
        topic.setTname(request.getParameter("tname"));
        request.getSession().setAttribute("topicInfo",topicService.updateTopic(topic));
        response.sendRedirect("/topic.do?opr=getAll");
    }

    /**
     * 查询所有主题主题
     * @param request
     * @param response
     */
    public void getAll(HttpServletRequest request,HttpServletResponse response) throws SQLException,IOException {
        request.getSession().setAttribute("topicList",topicService.getAll());
        response.sendRedirect("/admin/topic_list.jsp");
    }
}
